package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.FeedBackDataBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 09 月 21.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 */
public class FeedBackActivity extends Activity {
    private static final String TAG = "FeedBackActivity";
    @Bind(R.id.activity_feed_back_iv_return)
    ImageView activityFeedBackIvReturn;
    @Bind(R.id.activity_feed_back_tv_submit)
    TextView activityFeedBackTvSubmit;
    @Bind(R.id.activity_feed_back_tv_feed_back)
    TextView activityFeedBackTvFeedBack;
    @Bind(R.id.activity_feed_back_et_feed_back)
    EditText activityFeedBackEtFeedBack;
    private FeedBackDataBean feedBackDataBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feed_back);
        ButterKnife.bind(this);


        Utils.setStatusBar(this, false, false);
        Utils.setStatusTextColor(true, this);

        activityFeedBackEtFeedBack.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                activityFeedBackTvFeedBack.setText("");
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @OnClick({R.id.activity_feed_back_iv_return, R.id.activity_feed_back_tv_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_feed_back_iv_return:
                finish();
                break;
            case R.id.activity_feed_back_tv_submit:
                String feadback = activityFeedBackEtFeedBack.getText().toString().trim();
                if(!TextUtils.isEmpty(feadback)){
                    SharedPreferences sp =getSharedPreferences("second_app_internal_data", 0);
                    String user_id = sp.getString("user_id","");
                    submitFeedBackData(feadback,user_id);
                }else{
                    Toast.makeText(this,"请输入反馈信息",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
    /**
     * @desc 提交意见反馈
     * @param feadback
     * @param user_id*/
    private void submitFeedBackData(String feadback, String user_id) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.FEED_BACK)
                .addParams("cus_id",user_id)
                .addParams("cat_id", "123")
                .addParams("feedback",feadback)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String praiseParseNetworkResponse =  response.body().string();
                        Gson gson = new Gson();
                        Log.e(TAG,"praiseParseNetworkResponse============>"+praiseParseNetworkResponse);
                        feedBackDataBean = gson.fromJson(praiseParseNetworkResponse,FeedBackDataBean.class);
                        MyApplication.getmHandler().post(new Runnable() {
                            @Override
                            public void run() {
                                if(feedBackDataBean.errorCode == 200){
                                    Toast.makeText(FeedBackActivity.this, "发布成功", Toast.LENGTH_SHORT).show();
                                    finish();
                                    overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                                }else{
                                    Toast.makeText(FeedBackActivity.this, "发布失败", Toast.LENGTH_SHORT).show();
                                }
                            }
                        });

                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(FeedBackActivity.this, "发布失败", Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });
    }
}
